% Cohort and Duration Transprob cacluation with conditions:
% Rates {1+1.5, 2,2.5,3,3.5,4, 4.5+5+5.5, -1};
% maxPPDOS_sum constrains is controlled by variable 'conditions' in loop
% cdIdex

clear all
clc

conn = database('BDC','','');
labels = {1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,-1};
edges = [2 3 4 5 6 7 10 11 12];
%edgesDur = [2 3 4 5 6 7 10 11];

year_list=1997:2009;
DURATION = 1;
NUMBER_OF_YEAR = length(year_list) - DURATION;
conditions = {'maxPPDOS_sum >= 50000','maxPPDOS_sum >= 50000 and maxPPDOS_sum < 250000',...
    'maxPPDOS_sum >= 250000 and maxPPDOS_sum <1000000','maxPPDOS_sum >= 1000000'};
numRate = length(labels)+1; % add column for WR group
numEdges = length(edges);
%numEdgesStd = numEdges-1;
numGrp = length(conditions);

ttlRecord(numGrp, NUMBER_OF_YEAR,1)= struct('totalsVec',[],'totalsMat',[],'algorithm',[]); 
TpGrpRecord(numGrp, NUMBER_OF_YEAR,1)= struct('totalsVec',[],'totalsMat',[],'algorithm',[]); 
cohTp = zeros(numGrp, NUMBER_OF_YEAR, numRate, numRate);
cohTpGrp = zeros(numGrp, NUMBER_OF_YEAR, numEdges, numEdges);
default = zeros(numGrp, NUMBER_OF_YEAR,1);
nonRateNum = zeros(numGrp, NUMBER_OF_YEAR,1);
ttlNum = zeros(numGrp, NUMBER_OF_YEAR,1);
%avgCohTpGrp = zeros(numGrp,numEdges, numEdges);


for cdIdx = 1:numGrp
    disp(conditions(cdIdx));
    for yearIdx=1:NUMBER_OF_YEAR
        
        Date1 = strcat(num2str(year_list(yearIdx)),'-12-01');
        Date2 = strcat(num2str(year_list(yearIdx+DURATION)),'-12-01');
        disp( strcat(['Date: ' Date1 ' to ' Date2]));
        
        condition = char(conditions(cdIdx));
        sqlquery1 = strcat(['select Customer, modiFisDate, modiRate FROM final_v4_tbl where ' condition ' and modiFisDate BETWEEN ''' Date1 ''' and ''' Date2 ''' order by customer, modiFisDate']);
        results = fetch(conn, sqlquery1);
        disp(size(results));
        
        % cohort
        [cohTp(cdIdx,yearIdx,:,:),ttl] = transprobwr(results, 'labels',labels,'algorithm','cohort');
        TpGrp = transprobgrouptotals(ttl,edges);
        cohTpGrp(cdIdx,yearIdx,:,:) = transprobbytotalswr(TpGrp);
        
        ttlRecord(cdIdx,yearIdx,1) = ttl;
        TpGrpRecord(cdIdx,yearIdx,1) = TpGrp;
               
        default(cdIdx,yearIdx,1) = sum(TpGrp.totalsMat(1:numEdges-2,numEdges-1));
        nonRateNum(cdIdx,yearIdx,1) = sum(TpGrp.totalsMat(1:numEdges-2,numEdges));
        ttlNum(cdIdx,yearIdx,1) = sum(TpGrp.totalsVec(:,1:numEdges-2));
                 
        disp('Cohort tranprob is done')
        
    end  
    
end

nonRatePcg = nonRateNum./ttlNum;
defaultProb = default./ttlNum;

cohTtlVec = zeros(cdIdx,yearIdx,1, numEdges);
cohTtlMat = zeros(cdIdx,yearIdx,numEdges, numEdges);
avgCohTpGrp = zeros(cdIdx,numEdges, numEdges);

for cdIdx = 1:numGrp
    for yearIdx=1:NUMBER_OF_YEAR    
    %To calculate average tranprob   
    TpGrp = TpGrpRecord(cdIdx,yearIdx,1);
    cohTtlVec (cdIdx,yearIdx,:,:) = TpGrp.totalsVec;
    cohTtlMat (cdIdx,yearIdx,:,:) = TpGrp.totalsMat;
    end
    
    avgCohTotalVec(:,:) = sum(cohTtlVec(cdIdx,:,:,:),2);
    avgCohTotalMat(:,:) = sum(cohTtlMat(cdIdx,:,:,:),2);
    sampleTotals = struct('totalsVec',avgCohTotalVec,'totalsMat',avgCohTotalMat,'algorithm','cohort');
    avgCohTpGrp(cdIdx,:,:) = transprobbytotals(sampleTotals);
end


disp('finish')
save ('D:\My Documents\CIRANO\db_mySQL\tp_2011Mar5_MR7_wr_NR_Coh.mat')

%% block to calculate Q for specific transitions
% diagInd = logical(eye(numEdges,numEdges));
% totalsVec(:,:) = durTtlVec (grp,yr,1,:);
% totalsMat(:,:) = durTtlMat (grp,yr,:,:);
% totalsMat(diagInd) = 0;
% totalsMat(diagInd) = -sum(totalsMat,2);
% nonEmpty = find(totalsVec>0);
% transMat(nonEmpty,:) = bsxfun(@ldivide,...
% totalsVec(nonEmpty),totalsMat(nonEmpty,:));